<?php
/**
 * Created by PhpStorm.
 * User: zhang
 * Date: 2018/4/30
 * Time: 12:42
 */
namespace app\doctor\controller;

use app\common\model\Base;

class Doctorcasecomment extends Base{

    public function index(){
        $action = $_GET["action"];
        $case_id = $_GET["case_id"];
        if ($this->check->isGoodsNull($action, $case_id) == true) {
            $this->getAction($action, $case_id);
        }
    }

    /**
     *  判断是删除还是加入
     * @param str $uid 账号
     */
    function getAction($action, $case_id) {
        if ($action == 'all_comment') {
            $this->getAllComment($case_id);
        } else if ($action == "delete_comment") {
            //if (isMD5Verify() === true) {////验证MD5
                $this->deleteComment($case_id);
            //} else {
            //    echo echoJson(0, "数据验证失败!");
           // }
        } else if ($action == 'comment') {//评论
            //if (isMD5Verify() === true) {////验证MD5
                $this->setComment($case_id);
            //} else {
            //    echo echoJson(0, "数据验证失败!");
            //}
        } else {
            echo echoJson(0, "参数错误，-003!");
        }
    }

    /**
     * 得到所有评论
     * $case_id 案例id
     */
    function getAllComment($case_id) {
        $sql = "SELECT  A.comment_id,A.content,A.datetime,B.uid,B.nickname,B.nickname,B.headpic FROM doctor_case_comment "
            . "as A LEFT JOIN user as B on A.uid = B.uid  WHERE case_id=$case_id "
            . "ORDER BY A.datetime DESC";
        $result = $this->mysql->selectSQLAll($sql);
        if (is_array($result)) {
            $Info = array();
            foreach ($result as $row) {
                if (isset($row["comment_id"]) && isset($row["uid"]) && isset($row["nickname"]) && isset($row["content"]) && isset($row["datetime"])) {
                    $Info[] = array("id" => $row["comment_id"], "uid" => $row["uid"], "nickname" => $row["nickname"],
                        "headpic" => $row["headpic"], "content" => $row["content"], "datetime" => $row["datetime"]);
                }
            }
            if (count($Info) > 0) {
                echo echoJson(1, $Info);
            } else {
                echo echoJson(0, "暂无记录!");
            }
        } else {
            echo echoJson(0, "暂无记录!");
        }
    }

    /**
     * 设置评论
     * $case_id 案例id
     */
    function setComment($case_id) {
        $uid = $_GET["uid"];
        $content = $_GET["content"];
        if (!empty($uid) && !empty($content)) {
            $datetime = date("Y-m-d H:i:s");
            $sql = "INSERT INTO doctor_case_comment (case_id,uid,content,datetime) VALUES ($case_id,'$uid','$content','$datetime')";
            if ($this->mysql->insertSQL($sql)) {
                echo echoJson(1, "评论成功!");
            } else {
                echo echoJson(0, "评论失败!");
            }
        } else {
            echo echoJson(0, "参数错误，-004!");
        }
    }

    /**
     * 删除评论
     * $case_id 案例id
     */
    function deleteComment($case_id) {
        $id = $_GET["id"];
        if (!empty($id)) {
            $sql = "DELETE FROM doctor_case_comment WHERE case_id='$case_id' AND comment_id=$id";
            if ($this->mysql->deleteSQL($sql)) {
                echo echoJson(1, "删除成功!");
            } else {
                echo echoJson(0, "删除失败!");
            }
        } else {
            echo echoJson(0, "参数错误，-005!");
        }
    }
}